package cn.xuan.scan.hello.hcontroller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSON;
import net.sf.json.util.JSONUtils;

@Controller
@RequestMapping("/html")
public class PageController {
	private static final Logger logger = LoggerFactory.getLogger(PageController.class);

	@ResponseBody
	@RequestMapping("login")
	public Map<String, Object> login(HttpServletRequest req,HttpServletResponse resp) {
		Map<String, Object> map = new HashMap<String, Object>();
		
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		   token.setRememberMe(true);
		   /*String rand=(String)SecurityUtils.getSubject().getSession().getAttribute("rand");*/
	       /*if(!rand.equals(userValidate.getCode())){
	    	   return new MSG("error code");
	       }*/
		   try {
			   SecurityUtils.getSubject().login(token);
			   map.put("info", "login success");
			   return map;
			} catch ( UnknownAccountException uae ) {
				 map.put("info", "error username");
				   return map;
			} catch ( IncorrectCredentialsException ice ) {
				 map.put("info", "error password");
				   return map;
			} catch ( LockedAccountException lae ) {
				 map.put("info", "locked user");
				   return map;
			}
		
	}

	@ResponseBody
	@RequestMapping("unauthorized")
	public Map<String, Object> unauthorized() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "unauthorized");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

	@RequiresRoles("admin")
	@ResponseBody
	@RequestMapping("roleadmin")
	public Map<String, Object> roleadmin() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "roleadmin");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

	@ResponseBody
	@RequestMapping("permissionadmin")
	public Map<String, Object> permissionadmin() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "permissionadmin");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

	@ResponseBody
	@RequestMapping("useradmin")
	public Map<String, Object> useradmin() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "useradmin");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

	@ResponseBody
	@RequestMapping("index")
	public Map<String, Object> index() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pages", "index");
		logger.info(com.alibaba.druid.support.json.JSONUtils.toJSONString(map));
		return map;
	}

}
